using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameMainPanel : MonoBehaviour
{
    public GamePlay m_GamePlay;
    public Button m_BtnReturn;
    public Button m_BtnPullBack;
    public Text m_TxtPullBackCount;
    public Text m_TxtLeftMineCount;
    public Text m_TxtPassTime;
    public Button m_BtnNewGame;
    public Toggle m_TogDig;
    public Toggle m_TogFlag;
    public Toggle m_TogTip;
    public Toggle m_TogQuickMark;
    public Button m_BtnMusic;

    public float PassTime
    {
        get { return m_PassTime; }
    }
    public int LeftMineCount
    {
        get { return m_LeftMineCount; }
    }
    public int LeftPullBackCount
    {
        get { return m_LeftPullBackCount; }
        // set { m_LeftPullBackCount = value; }
    }

    public void ModifyLeftMineCount(bool add)
    {
        m_LeftMineCount = add ? m_LeftMineCount + 1 : m_LeftMineCount - 1;
        m_TxtLeftMineCount.text = m_LeftMineCount.ToString();
    }

    public void StartPassTimer(ArchiveMgr.Archive archive)
    {
        StopPassTimer();
        m_PassTimerCo = StartCoroutine(PlayTimer(archive));
    }

    public void StopPassTimer()
    {
        if (m_PassTimerCo != null)
            StopCoroutine(m_PassTimerCo);
        m_PassTimerCo = null;
    }

    public void UpdateLeftMineCount(int leftMineCount)
    {
        m_LeftMineCount = leftMineCount;
        m_TxtLeftMineCount.text = m_LeftMineCount.ToString();
    }

    public void UpdatePullBack()
    {
        m_BtnPullBack.gameObject.SetActive(m_LeftPullBackCount > 0 && m_GamePlay.CheckIsLose());
        int maxCount = GameMode.Instance.GetCurPullBackCount();
        m_TxtPullBackCount.gameObject.SetActive(maxCount > 0);
        m_TxtPullBackCount.text = string.Format("剩余复活:{0}/{1}", m_LeftPullBackCount, maxCount);
    }

    public void UpdatePullBack(int leftPullBackCount)
    {
        m_LeftPullBackCount = leftPullBackCount;
        UpdatePullBack();
    }

    public void SelectOperation(GamePlay.EGameOperation gameOperation)
    {
        if (gameOperation == GamePlay.EGameOperation.eDig)
        {
            m_TogDig.SetIsOnWithoutNotify(true);
            OnTogDigValueChange(true);

        }
        else if (gameOperation == GamePlay.EGameOperation.eFlag)
        {
            m_TogFlag.SetIsOnWithoutNotify(true);
            OnTogFlagValueChange(true);
        }
        else
        {
            m_TogTip.SetIsOnWithoutNotify(true);
            OnTogTipValueChange(true);
        }
    }

    //=========================================公私分界线==================================================

    private int m_LeftMineCount;
    private float m_PassTime;
    private int m_LeftPullBackCount;
    private Coroutine m_PassTimerCo;

    void Awake()
    {
        m_BtnReturn.onClick.AddListener(OnBtnReturnClick);
        m_BtnPullBack.onClick.AddListener(OnBtnPullBackClick);
        m_BtnNewGame.onClick.AddListener(OnBtnNewGameClick);
        m_TogDig.onValueChanged.AddListener(OnTogDigValueChange);
        m_TogFlag.onValueChanged.AddListener(OnTogFlagValueChange);
        m_TogTip.onValueChanged.AddListener(OnTogTipValueChange);
        m_TogQuickMark.onValueChanged.AddListener(OnTogQuickMarkChange);
        m_BtnMusic.onClick.AddListener(OnBtnMusicClick);
    }

    void Start()
    {
        UpdateMusicBtn();
    }

    void OnBtnReturnClick()
    {
        m_GamePlay.LeaveGame();
    }

    void OnBtnPullBackClick()
    {
        m_GamePlay.PullBack();
    }

    void OnBtnNewGameClick()
    {
        m_GamePlay.NewGame();
    }

    void OnTogDigValueChange(bool value)
    {
        if (value)
            m_GamePlay.ChangeOperation(GamePlay.EGameOperation.eDig);
    }

    void OnTogFlagValueChange(bool value)
    {
        if (value)
            m_GamePlay.ChangeOperation(GamePlay.EGameOperation.eFlag);
    }

    void OnTogTipValueChange(bool value)
    {
        if (value)
            m_GamePlay.ChangeOperation(GamePlay.EGameOperation.eTip);
    }

    void OnTogQuickMarkChange(bool value)
    {
        m_GamePlay.SwitchQuickMark(value);
    }

    void OnBtnMusicClick()
    {
        AudioManager.Instance.SwitchMusic();
        UpdateMusicBtn();
    }

    void UpdateMusicBtn()
    {
        bool musicFlag = AudioManager.Instance.MusicFlag;
        m_BtnMusic.transform.GetChild(0).gameObject.SetActive(musicFlag == true);
        m_BtnMusic.transform.GetChild(1).gameObject.SetActive(musicFlag == false);
    }

    IEnumerator PlayTimer(ArchiveMgr.Archive archive)
    {
        float historyTime = archive != null ? archive.Time : 0f;
        float startTime = Time.time - historyTime;
        while (true)
        {
            m_PassTime = Time.time - startTime;
            m_TxtPassTime.text = Mathf.FloorToInt(m_PassTime).ToString();
            yield return null;
        }
    }


}
